<template>
  <page-dialog
    :title="dialogInfo.title"
    v-model:visible="shows"
    :width="dialogInfo.width"
    :bt-list="dialogInfo.btList"
  >
    <!-- 条件栏 -->
    <NewPageFilter
      :query="filterInfo.query.queryMap"
      :query-more-json="true"
      :filter-list="filterInfo.searchList"
      :filter-btn="filterInfo.searchBtn"
    >
      <template v-slot:filter-supplierProdct>
        <slip-select
          v-model="filterInfo.query.queryParams.notDefaultProductSupplierId"
          value-key="value"
          placeholder="选择供货供应商"
          :api="supplierInfo.api"
          :query="supplierInfo.query"
          key-field="supplierName"
          value-field="supplierId"
          :clearable="true"
          :filterable="true"
          :multiple="true"
          @change="search"
        />
      </template>
    </NewPageFilter>
    <el-form ref="tableForm" :model="tableInfo" style="width: 100%">
      <dialog-table
        ref="resourceTable"
        :refresh="tableInfo.refresh"
        :init-curpage="tableInfo.initCurpage"
        v-model:data="tableInfo.data"
        :api="tableInfo.api"
        :query="filterInfo.query"
        :field-list="tableInfo.fieldList"
        :handle="tableInfo.handle"
        :row-key="rowKey"
        :highlight-row="highlightRow"
        :checked-row-list="checkedRowList"
        :reserve-selection="true"
        :check-box="checkBox"
        @handleEvent="handleEvent"
      >
        <template v-slot:col-editQty="scope">
          <el-form-item class="margin-bottom" @click.stop="">
            <div class="page-table-input-number-wrapper">
              <div class="page-table-input-number-left">
                <div class="page-table-input-number-item">
                  <el-form-item>
                    <el-input-number
                      v-model.lazy="scope.row.editQty"
                      style="width: 100%; margintop: 18px"
                      clearable
                      controls-position="right"
                      :precision="0"
                      :min="0"
                      :max="99999999"
                    />
                  </el-form-item>
                </div>
              </div>
            </div>
          </el-form-item>
        </template>
      </dialog-table>
    </el-form>
  </page-dialog>
</template>
<script setup name="selectWithTableKit">
import PageDialog from "../PageDialog/index";
import DialogTable from "../DialogTable/index";
import SlipSelect from "../SlipSelect/index";
import { getCustomizeSetting } from "@/api/customize";
import { getSupplierAll } from "@/api/core.js";
import { getWithTableKitCustomizeListPage } from "@/api/withTable.js";


const { proxy } = getCurrentInstance();
const emit = defineEmits(["update", "callback", "reset"]);

const props = defineProps({
  highlightRow: {
    type: Boolean,
    default: false,
  },
  checkBox: {
    type: Boolean,
    default: true,
  },
  rowKey: {
    type: String,
    default: "id",
  },
  api: {
    type: Function,
    default: getWithTableKitCustomizeListPage,
  },
  showDialog: {
    type: Boolean,
    default: false,
  },
  showPrice: {
    type: Boolean,
  },
  showSupplierName: {
    type: Boolean,
    default: false,
  },
  notDefaultProductSupplier: {
    //是否供货供应商赛选
    type: Boolean,
    default: true,
  },
  query: {
    type: Object,
    default: function () {
      return {};
    },
  },
  queryParams: {
    type: Object,
    default: function () {
      return {};
    },
  },
  showStock: {
    type: Boolean,
    default: false,
  },
  tableKey: {
    type: String,
    default: "",
  },
  checkedList: {
    type: Array,
    default: () => {
      return [];
    },
  },
  checkedRowList: {
    type: Array,
    default: () => {
      return [];
    },
  },
  saveEmpty: {
    type: Boolean,
    default: false,
  },
});

const supplierInfo = reactive({
  api: getSupplierAll,
  query: {
    supplierAuth: "supplierId",
    sort: "create_time desc",
  },
});
// 过滤相关配置
const filterInfo = reactive({
  query: {
    page: true,
    customizeUserVO: {},
    sortString: "",
    queryMap: {
      status: {
        queryType: "EQUALS",
        queryCategory: "AND",
        value: 1,
      },
    },
    queryParams: {},
  },
  searchList: [],
  searchBtn: [{ type: "button", label: "搜索", event: search }],
});
// 表格相关
const tableInfo = reactive({
  refresh: 1,
  initCurpage: 1,
  data: [],
  api: undefined,
  fieldList: [],
});
// 弹窗相关
const dialogInfo = reactive({
  title: "选择手术包",
  width: "90%",
  btLoading: false,
  btList: [
    { label: "关闭", event: handleClose, show: true },
    {
      label: "保存",
      type: "primary",
      disabled: !props.saveEmpty,
      event: save,
      show: true,
    },
  ],
});
const state = reactive({
  selectRow: [],
});

const shows = computed({
  get() {
    return props.showDialog;
  },
  set(newValue) {
    emit("update:showDialog", newValue); // 更新父组件shows
  },
});

let obj = {
  statusValue: {
    label: "状态",
    value: "statusValue",
    asValue: "status",
    componentType: "select-model",
    code: "enabled_type",
    show: false,
    hidden: true,
  },
  name: {
    label: "名称",
    value: "name",
    componentType: "input",
    show: false,
    hidden: true,
  },
  price: {
    label: "单价",
    value: "price",
    hidden: true,
    type: "column",
    show: false,
    permission: !props.showPrice,
    formatter: proxy.$fn.TripartiteMethod,
  },
  defaultSupplierPrice: {
    label: "默认供应商价格",
    value: "defaultSupplierPrice",
    type: "column",
    permission: !props.showPrice,
    show: false,
    formatter: proxy.$fn.TripartiteMethod,
    hidden: true,
  },
  showPrice: {
    label: "展示价格",
    value: "showPrice",
    type: "column",
    show: false,
    permission: !props.showPrice,
    formatter: proxy.$fn.TripartiteMethod,
    hidden: true,
  },
  commonName: {
    label: "通用名称",
    value: "commonName",
    componentType: "input",
    show: false,
    hidden: true,
  },
  editQty: {
    label: "数量",
    value: "editQty",
    type: "slot",
    componentType: "number",
    show: false,
    hidden: true,
    minWidth: "250px",
  },
};
tableInfo.fieldList = Object.values({
  ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
  ...proxy.$fn.deepClone(obj),
});
filterInfo.searchList = Object.values({
  ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
  ...proxy.$fn.deepClone(obj),
});
getCustomize();

// 获取列表
function getList () {
  tableInfo.refresh = Math.random();
};
function search () {
  tableInfo.initCurpage = Math.random();
  tableInfo.refresh = Math.random();
};
function save () {
  state.selectRow.forEach((item) => {
    item["calcQty"] = item.editQty;
    item["qty"] = item.editQty;
  });
  handleClose();
  emit("callback", state.selectRow);
};
function handleEvent (event, data) {
  switch (event) {
    case "tableClickRow":
      state.selectRow = data;
      if (!props.saveEmpty) {
        dialogInfo.btList[1].disabled = !state.selectRow;
      }
      break;
    case "tableCheck":
      state.selectRow = data;
      if (!props.saveEmpty) {
        dialogInfo.btList[1].disabled = !state.selectRow;
      }
      break;
  }
};
// 关闭弹窗前的回调
function handleClose () {
  shows.value = false;
  emit("reset");
};
function getCustomize () {
  
  
  let param = { tableKey: props.tableKey };
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data;
      tableInfo.fieldList = proxy.$fn.setFieldList(
        tableInfo.fieldList,
        data.detail
      );
      let searchInfo = proxy.$fn.setSearchList(
        filterInfo.searchList,
        data.detail
      );
      let supplierProdct = {
        slot: true,
        value: "supplierProdct",
      };
      if (props.notDefaultProductSupplier) {
        filterInfo.searchList = searchInfo.searchList.concat(supplierProdct);
      } else {
        filterInfo.searchList = searchInfo.searchList;
      }
      filterInfo.searchList.forEach((item) => {
        item.event = search;
      });

      filterInfo.query.customizeUserVO = data;
      filterInfo.query.queryMap = {
        ...searchInfo.queryMap,
        ...props.query,
        ...filterInfo.query.queryMap,
      };
      filterInfo.query.queryParams = {
        ...filterInfo.query.queryParams,
        ...props.queryParams,
      };
      tableInfo.api = props.api;
      console.log(tableInfo.fieldList);
      getList();
    }
  });
};
</script>

<style scoped lang="scss"></style>
